#include "thrd_cup.h"

int CThrdCup::Init(int iThrd)
{
    int iRet = -1;

    if (iThrd < 1) SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);

    iRet = Init(1, iThrd, 10);
_Exit:
    return iRet;
}

int CThrdCup::Recycle()
{
    Destruct();
    return Construct();
}

int CThrdCup::Start()
{
    return 0;
}

int CThrdCup::Stop()
{
    return 0;
}

int CThrdCup::AddTask(IThrdTask* pobjTask)
{
    return 0;
}

int CThrdCup::Construct()
{
    m_iDepth = m_iDepthMax = m_iQuLenMax = -1;
    m_pobjNext = NULL;
    m_pobjThrd = NULL;
    return 0;
}

int CThrdCup::Destruct()
{
    Stop();

    while (m_objQueue.size() > 0)
    {
        auto p = m_objQueue.front();
        DELETE_AND_NULL(p);
        m_objQueue.pop();
    }

    return 0;
}

int CThrdCup::Init(int iDepth, int iDepthMax, int iQueue)
{
    int iRet = -1;
    int iResult, iTmp;
    int i;
    CThrdCup* pclNext = NULL;

    if (iDepthMax < 1) SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);

    m_iDepth = iDepth;
    m_iDepthMax = iDepthMax;
    m_iQuLenMax = iQueue;

    if (iDepth < iDepthMax)
    {
        pclNext = new CThrdCup();
        iResult = pclNext->Init(m_iDepth + 1, m_iDepthMax, 3);
        if (iResult < 0) GOTO_EXIT;
    }

    iRet = 0;
_Exit:
    if (iRet < 0) {
        DELETE_AND_NULL(pclNext);
    }
    return iRet;
}
